package com.blq.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.blq.common.utils.wx.entity.TemplateDataVo;
import com.blq.common.utils.wx.entity.WxMssVo;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送小程序模板消息
 *
 * @author: Blq <br>
 */
public class WecatTempleat {
    public static JSONObject WXuserinfo(String openid, String templateid, String accesstoken, String[] keywords) {

        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send" +
                "?access_token=" + accesstoken;
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);
        wxMssVo.setForm_id("");
        wxMssVo.setTemplate_id(templateid);
        wxMssVo.setPage("");
        Map<String, TemplateDataVo> templateDataVoMap = new HashMap<>();
        if (keywords.length > 0) {
            for (int i = 1; i <= keywords.length; i++) {
                TemplateDataVo keyword = new TemplateDataVo();
                keyword.setValue(keywords[i - 1]);
                switch (i) {
                    case 1:
                        templateDataVoMap.put("name1", keyword);
                        break;
                    case 2:
                        templateDataVoMap.put("date2", keyword);
                        break;
                    case 3:
                        templateDataVoMap.put("name3", keyword);
                        break;
                    case 4:
                        templateDataVoMap.put("date4", keyword);
                        break;
                    case 5:
                        templateDataVoMap.put("phrase6", keyword);
                        break;
                }
            }
            wxMssVo.setData(templateDataVoMap);
            com.alibaba.fastjson.JSONObject post = WeChatUtil.httpRequest(url, "POST", com.alibaba.fastjson.JSONObject.toJSONString(wxMssVo));
            return post;
        }
        return null;
    }
}
